/**
 * 本文件是一个工厂模式
 */

 class Circle{
     draw(){
         console.log('draw circle!')
     }
 }

 class Triangle{
    draw(){
        console.log('draw triangle!')
    }
}

class Rectangle{
    draw(){
        console.log('draw rectangle!')
    }
}

//形状工厂
class ShapeFactory{
    getShape(shape){
        switch(shape){
            case 'CIRCLE': return new Circle()
            case 'TRIANGLE': return new Triangle()
            case 'RECTANGLE': return new Rectangle()
            default: return null
        }
    }
}

// const shapeFactory = new ShapeFactory()
// let shape = shapeFactory.getShape('CIRCLE')
// shape.draw()

// 再新加一个颜色工厂
class Red {
    fill() {
        console.log("fill red")
    }
}
class Blue {
    fill() {
        console.log("fill blue")
    }
}
class Green {
    fill() {
        console.log("fill green")
    }
}

class ColorFactory {
    getColor(color){
        switch(color) {
            case 'RED':
                return new Red();
            case 'BLUE':
                return new Blue();
            case 'GREEN':
                return new Green();
            default:
                return null;
        }
    }
}

// 最后添加抽象工厂
class FactoryProducer {
    static getFactory(choice){
        switch(choice) {
            case 'SHAPE':
                return new ShapeFactory();
            case 'COLOR':
                return new ColorFactory();
            default:
                return null;
        }
    }
}

